package com.jiamao.goplay.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;

import com.jiamao.goplay.R;
import com.jiamao.goplay.ui.discover.vm.CityMenuItemVM;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import skin.support.content.res.SkinCompatResources;

public class CityMenuAdapter extends BindingRecyclerViewAdapter {

    Context context;
    public CityMenuAdapter(Context context){
        this.context = context;
    }

    @Override
    public ViewDataBinding onCreateBinding(LayoutInflater inflater, @LayoutRes int layoutId, ViewGroup viewGroup) {
        ViewDataBinding binding = super.onCreateBinding(inflater, layoutId, viewGroup);
        return binding;
    }

    private int iondex = 0 ;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindBinding(ViewDataBinding binding, int bindingVariable, @LayoutRes int layoutId, int position, Object item) {
        super.onBindBinding(binding, bindingVariable, layoutId, position, item);
                if (item instanceof CityMenuItemVM){

                CityMenuItemVM vm  = (CityMenuItemVM) item;

                TextView textView  = binding.getRoot().findViewById(R.id.tv_title);
                View view  = binding.getRoot().findViewById(R.id.iv_select);
                    if(vm.select.get()){
                        int defaule = SkinCompatResources.getInstance().getColor(context,R.color.discover_city_menu_title_select);
                        int defaule1 = SkinCompatResources.getInstance().getColor(context,R.color.search_btn);
                        binding.getRoot().setBackgroundColor(defaule);
                        textView.setTextColor(defaule1);
                        view.setVisibility(View.VISIBLE);
                    }else {

                        int defaule = SkinCompatResources.getInstance().getColor(context,R.color.discover_city_menu_bg);
                        int defaule1 = SkinCompatResources.getInstance().getColor(context,R.color.titlebar_title);
                        binding.getRoot().setBackgroundColor(defaule);
                        textView.setTextColor(defaule1);
                        view.setVisibility(View.GONE);
                    }

                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (iondex ==position){
                            return;
                        }
                        vm.select.set(true);
                        refreshUI();
                        iondex = position;
                        vm.itemClick.execute();


                    }
                });
             }
    }

    public void refreshUI(){

        CityMenuItemVM vm = (CityMenuItemVM) getAdapterItem(iondex);
        vm.select.set(false);
//        notifyItemChanged(iondex);
        notifyDataSetChanged();

    }

//    @Override
//    public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, int layoutRes, int position, Object item) {
//        super.onBindBinding(binding, variableId, layoutRes, position, item);
//
//        if (item instanceof CityMenuItemVM){
//            CityMenuItemVM vm  = (CityMenuItemVM) item;
//
//        }
//
//    }
}
